package com.power.sifa.statistics.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.statistics.api.IApl_note_bookFeign;
import com.power.sifa.statistics.model.Apl_note_book;
import com.power.sifa.statistics.model.pageInfo.Apl_note_bookPageInfo;
import com.power.sifa.statistics.service.Apl_note_bookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   每日记录记事本控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_note_bookController",tags={"每日记录记事本"})
public class Apl_note_bookController implements IApl_note_bookFeign {

    @Resource
    private Apl_note_bookService apl_note_bookService;


    @Override
    @ApiOperation("查询每日记录记事本分页列表")
    @SaCheckPermission("apl_note_book_list")
    public ResponseDTO list(Apl_note_bookPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_note_book> page = this.apl_note_bookService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_note_book apl_note_book : page) {
                apl_note_book.geneText();
                list.add(apl_note_book);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看每日记录记事本")
    @SaCheckPermission("apl_note_book_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_note_book apl_note_book  = apl_note_bookService.getById(id);
        if (apl_note_book != null) {
            apl_note_book.geneText();
        }
        return ResponseDTO.succData(apl_note_book);
    }

    @Override
    @ApiOperation("删除每日记录记事本")
    @SaCheckPermission("apl_note_book_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_note_bookService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改每日记录记事本")
    @SaCheckPermission("apl_note_book_save")
    public ResponseDTO save(Apl_note_book apl_note_book) {
        Assert.notNull(apl_note_book, "apl_note_book不能为空");
        apl_note_bookService.saveOrUpdate(apl_note_book);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除每日记录记事本")
    @SaCheckPermission("apl_note_book_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_note_bookService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_note_book_list")
    public void output(HttpServletResponse response, Apl_note_bookPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "每日记录记事本";
        String[] heads = {"nanoid","用户id","用户名称","记录时间","记录文本"};
        String[] fields = {"id","userid","username","notetime","notetext"};
        Page<Apl_note_book> page = this.apl_note_bookService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "每日记录记事本";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"nanoid","用户id","用户名称","记录时间","记录文本"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","userid","username","notetime","notetext"};
        List<Apl_note_book> list = (List<Apl_note_book>)ExcelUtil.Import(fields, file, Apl_note_book.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_note_bookService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    /**
     * 每日记录记事本-查询当前登录用户全部填写的日期
     * @param userid
     * @return
     */
    @Override
    public ResponseDTO allRecordTime(String userid) {
        Assert.notNull(userid, "userid不能为空");
        return ResponseDTO.succData(this.apl_note_bookService.allRecordTime(userid));
    }

}
